﻿Imports ControladorasAT.SIREOS
Imports Utilitarios
Imports ServiciosAT
Imports System.Data

Partial Class Pages_Default
    Inherits System.Web.UI.Page

    Private selID As Decimal

    Private Sub ListarEmpleados()
        Dim oEmpleadoBE As New SireosWSEmpleado.Empleado
        oEmpleadoBE.codLocal = Convert.ToInt32(ddlbLocal.SelectedValue)
        oEmpleadoBE.codArea = Convert.ToInt32(ddlbArea.SelectedValue)
        oEmpleadoBE.codCargo = Convert.ToInt32(ddlbCargo.SelectedValue)
        oEmpleadoBE.nomEmpleado = txbEmpleado.Text
        oEmpleadoBE.nroDocumento = txbNroDocumento.Text
        oEmpleadoBE.estRegistro = Convert.ToInt32(ddlbEstado.SelectedValue)

        Dim oCtEmpleado As New CtEmpleado
        Dim dt As DataTable = oCtEmpleado.listarEmpleados(oEmpleadoBE)
        With gridEmpleado
            .Columns(0).Visible = True
            .DataSource = dt
            .DataBind()
            .Columns(0).Visible = False
        End With
        If Not dt Is Nothing Then
            LblTotal.Text = "Total: " + dt.Rows.Count.ToString + " registros"
        Else
            LblTotal.Text = "Total: 0 Registros"
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Listas.listarAreas(ddlbArea)
            Listas.listarLocales(ddlbLocal)
            Listas.listarCargos(ddlbCargo)
            Listas.listarTabla(Valores.Tabla.EstadoRegistro, ddlbEstado)
        End If
    End Sub

    Protected Sub ibtnAgregar_Click(sender As Object, e As ImageClickEventArgs) Handles ibtnAgregar.Click
        Response.Redirect("RRHHRegistroPersonal.aspx")
    End Sub

    Protected Sub imgBuscar_Click(sender As Object, e As ImageClickEventArgs) Handles imgBuscar.Click
        ListarEmpleados()
    End Sub

    Protected Sub gridEmpleado_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles gridEmpleado.PageIndexChanging
        ListarEmpleados()
        gridEmpleado.Columns(0).Visible = True
        gridEmpleado.PageIndex = e.NewPageIndex
        gridEmpleado.DataBind()
        gridEmpleado.Columns(0).Visible = False
    End Sub

    Protected Sub gridEmpleado_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gridEmpleado.RowDataBound
        Dim oImageButton As ImageButton = Nothing
        Try
            If e.Row.RowType = DataControlRowType.DataRow Then
                oImageButton = DirectCast(e.Row.FindControl("ibtnEditar"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
                oImageButton = DirectCast(e.Row.FindControl("ibtnAnular"), ImageButton)
                If oImageButton IsNot Nothing Then
                    oImageButton.CommandArgument = Convert.ToString(e.Row.RowIndex)
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub gridEmpleado_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gridEmpleado.RowCommand
        Try
            selID = Convert.ToInt32(gridEmpleado.Rows(Convert.ToInt32(e.CommandArgument)).Cells(0).Text)
            If e.CommandName = "cnEditar" Then
                Dim url As String = "RRHHRegistroPersonal.aspx?Codigo=" + selID.ToString + ",Modo=" + Valores.ModoPagina.Editar
                Response.Redirect(url)
            ElseIf e.CommandName = "cnAnular" Then
                Dim oCtEmpleado As New CtEmpleado
                Dim esta As String = gridEmpleado.Rows(Convert.ToInt32(e.CommandArgument)).Cells(7).Text
                If esta = Valores.EstadoRegistro.Activo.ToString Then
                    ' oCtEmpleado.desHabilitarEmpleado(selID, Valores.EstadoRegistro.Inactivo, Convert.ToInt32(Session("CodigoUsuario")))
                    Helper.MensajeAlerta(Me, Mensajes.correctoAnulado)
                Else
                    'oCtEmpleado.desHabilitarEmpleado(selID, Valores.EstadoRegistro.Activo, Convert.ToInt32(Session("CodigoUsuario")))
                    Helper.MensajeAlerta(Me, Mensajes.correctoHabilitado)
                End If
                ListarEmpleados()
            ElseIf e.CommandName = "cnUsuario" Then
                Dim url As String = "PersonalUsuario.aspx?Codigo=" + selID.ToString
                Response.Redirect(url)
            End If
        Catch ex As Exception
        End Try
    End Sub

End Class